package com.qqt.csr.order.service.statemachine.action.impl;

import com.qqt.csr.common.enums.YesOrNotEnum;
import com.qqt.csr.order.enums.WorkOrderEventEnum;
import com.qqt.csr.order.enums.WorkOrderStatusEnum;
import com.qqt.csr.order.enums.WorkOrderTemplateType;
import com.qqt.csr.order.service.event.DynamicEvent;
import com.qqt.csr.order.service.event.DynamicEventSource;
import com.qqt.csr.order.service.statemachine.action.IActionPostProcessor;
import com.qqt.csr.order.service.statemachine.action.IActionPreProcessor;
import com.qqt.csr.order.service.statemachine.context.WorkOrderContext;
import org.springframework.stereotype.Component;

@Component
public class ReopenedWorkOrderProcessor extends AbstractWorkOrderProcessor implements IActionPreProcessor, IActionPostProcessor {
    @Override
    public boolean matches(WorkOrderEventEnum event) {
        return WorkOrderEventEnum.REOPENED.equals(event);
    }

    @Override
    public void preProcess(WorkOrderStatusEnum from, WorkOrderStatusEnum to, WorkOrderEventEnum event, WorkOrderContext context) {
        // 重开工单，打上重开标记，及处理耗时重置
        context.getToOrder().setOpenAgainFlag(YesOrNotEnum.YES.getValue());
        context.getToOrder().setElapsedTime(0L);
    }

    @Override
    public void postProcess(WorkOrderStatusEnum from, WorkOrderStatusEnum to, WorkOrderEventEnum event, WorkOrderContext context) {
        sendChangedOrderStatusMsgNotify(context, WorkOrderTemplateType.REOPENED_WORK_ORDER, null);

        recordDynamic(context, event);
    }

    /**
     * 记录动态
     *
     * @param context
     */
    private void recordDynamic(WorkOrderContext context, WorkOrderEventEnum event) {
        DynamicEventSource eventSource = new DynamicEventSource()
                .setEvent(event)
                .setOrderId(context.getToOrder().getId());
        dynamicEventListener.onApplicationEvent(new DynamicEvent(eventSource));
    }


}
